package com.hcb.fragment;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hcb.adapter.AdapterAddContacts;
import com.hcb.app911.MainActivity;
import com.hcb.app911.R;
import com.hcb.app911.RegisterActivity;
import com.hcb.app911.RegisterActivity.VerySlowTaskExits;
import com.hcb.common.ActionEvent;
import com.hcb.constants.Constants;
import com.hcb.controller.CustomerController;
import com.hcb.dto.AlertDTO;
import com.hcb.dto.CustomerDTO;
import com.hcb.dto.ResponderDTO;
import com.hcb.dto.ResponderListDTO;
import com.hcb.location.DistanceLocation;
import com.hcb.model.utils.CustomerModel;
import com.hcb.webservice.InternetUtils;
import com.hcb.webservice.ServiceHandler;
import com.hcb.webservice.WebService;
import com.layout.util.CircularImageView;

public class HomeFragment extends Fragment implements OnClickListener {

	LinearLayout layouthd;

	View fotter;
	AdapterAddContacts adapter;
	CircularImageView imgaccident, imgcriticallyill, imgrobbery, imgfire,
			imgpregnancyhelp, imgdomestic, imgpersonalsafety, imgstranded;
	Button btnFineNow;
	String alert_type = "";
	String alert_name = "";
	DistanceLocation distanceLocation;
	private LocationManager locationManager = null;
	private Location locationF = null;
	private LocationListener locationListener = null;
	double lat;
	double lng;
	Dialog dialog, center_err;
	AnimationDrawable frameAnimation;
	String Address = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getVideoLocation();
		((MainActivity)this.getActivity()).getImgHome().setImageResource(R.drawable.iconf);
		((MainActivity)this.getActivity()).getImgDetail().setImageResource(R.drawable.mydetail);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_home, container,
				false);

		imgaccident = (CircularImageView) rootView
				.findViewById(R.id.imgaccident);
		imgcriticallyill = (CircularImageView) rootView
				.findViewById(R.id.imgcriticallyill);
		imgrobbery = (CircularImageView) rootView.findViewById(R.id.imgrobbery);
		imgfire = (CircularImageView) rootView.findViewById(R.id.imgfire);
		imgpregnancyhelp = (CircularImageView) rootView
				.findViewById(R.id.imgpregnancyhelp);
		imgdomestic = (CircularImageView) rootView
				.findViewById(R.id.imgdomestic);
		imgpersonalsafety = (CircularImageView) rootView
				.findViewById(R.id.imgpersonalsafety);
		imgstranded = (CircularImageView) rootView
				.findViewById(R.id.imgstranded);

		imgaccident.setOnClickListener(this);
		imgcriticallyill.setOnClickListener(this);
		imgrobbery.setOnClickListener(this);
		imgfire.setOnClickListener(this);
		imgpregnancyhelp.setOnClickListener(this);
		imgdomestic.setOnClickListener(this);
		imgpersonalsafety.setOnClickListener(this);
		imgstranded.setOnClickListener(this);

		layouthd = (LinearLayout) rootView.findViewById(R.id.abc);
		View footer = inflater.inflate(R.layout.fotter, container, false);

		layouthd.addView(footer);
		btnFineNow = (Button) footer.findViewById(R.id.btnFineNow);
		btnFineNow.setOnClickListener(this);

//		distanceLocation = new DistanceLocation(getActivity());
//		new Thread(new Runnable() {
//
//			@Override
//			public void run() {
//				double a = 10.817645, b = 106.466146;
//				try {
//					double c = distanceLocation.getDistance(a, b);
//					double i = c;
//				} catch (JSONException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//
//			}
//		}).start();
//		new GetCurrentAddress().execute();
		return rootView;

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (locationListener != null) {
			this.locationManager.removeUpdates(locationListener);
		}
	}

	private void getVideoLocation() {
		this.locationManager = (LocationManager) getActivity()
				.getSystemService(Context.LOCATION_SERVICE);

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.POWER_HIGH);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setSpeedRequired(false);
		criteria.setCostAllowed(true);

		String provider = locationManager.getBestProvider(criteria, true);

		this.locationListener = new LocationListener() {
			@Override
			public void onLocationChanged(Location location) {
				if (location != null) {
//					locationF = location;
					lat = location.getLatitude();
					lng = location.getLongitude();

					locationManager.removeUpdates(this);
				} else {

				}
			}

			@Override
			public void onProviderDisabled(String provider) {
			}

			@Override
			public void onProviderEnabled(String provider) {
			}

			@Override
			public void onStatusChanged(String provider, int status,
					Bundle extras) {
			}

		};

		if (provider != null) {
			locationManager.requestLocationUpdates(provider, 2000, 10,
					locationListener);
		}
	}

	private String getDateTime() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		return dateFormat.format(date);
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {

		case R.id.imgaccident:
			alert_type = "1";
			alert_name = "Accident";
			imgaccident
					.setBorderColor(getResources().getColor(R.color.skyblue));
			imgaccident.setBorderWidth(5);
			imgaccident.addShadow();
			imgcriticallyill.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgcriticallyill.setBorderWidth(5);
			imgcriticallyill.addShadow();

			imgrobbery.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgrobbery.setBorderWidth(5);
			imgrobbery.addShadow();

			imgfire.setBorderColor(getResources()
					.getColor(R.color.border_color));
			imgfire.setBorderWidth(5);
			imgfire.addShadow();

			imgpregnancyhelp.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpregnancyhelp.setBorderWidth(5);
			imgpregnancyhelp.addShadow();

			imgdomestic.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgdomestic.setBorderWidth(5);
			imgdomestic.addShadow();

			imgpersonalsafety.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpersonalsafety.setBorderWidth(5);
			imgpersonalsafety.addShadow();

			imgstranded.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgstranded.setBorderWidth(5);
			imgstranded.addShadow();

			break;
		case R.id.imgcriticallyill:
			alert_type = "2";
			alert_name = "Critically ill";
			imgaccident.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgaccident.setBorderWidth(5);
			imgaccident.addShadow();

			imgcriticallyill.setBorderColor(getResources().getColor(
					R.color.skyblue));
			imgcriticallyill.setBorderWidth(5);
			imgcriticallyill.addShadow();

			imgrobbery.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgrobbery.setBorderWidth(5);
			imgrobbery.addShadow();

			imgfire.setBorderColor(getResources()
					.getColor(R.color.border_color));
			imgfire.setBorderWidth(5);
			imgfire.addShadow();

			imgpregnancyhelp.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpregnancyhelp.setBorderWidth(5);
			imgpregnancyhelp.addShadow();

			imgdomestic.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgdomestic.setBorderWidth(5);
			imgdomestic.addShadow();

			imgpersonalsafety.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpersonalsafety.setBorderWidth(5);
			imgpersonalsafety.addShadow();

			imgstranded.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgstranded.setBorderWidth(5);
			imgstranded.addShadow();

			break;
		case R.id.imgrobbery:
			alert_type = "3";
			alert_name = "Robbery";

			imgaccident.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgaccident.setBorderWidth(5);
			imgaccident.addShadow();

			imgcriticallyill.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgcriticallyill.setBorderWidth(5);
			imgcriticallyill.addShadow();

			imgrobbery.setBorderColor(getResources().getColor(R.color.skyblue));
			imgrobbery.setBorderWidth(5);
			imgrobbery.addShadow();

			imgfire.setBorderColor(getResources()
					.getColor(R.color.border_color));
			imgfire.setBorderWidth(5);
			imgfire.addShadow();

			imgpregnancyhelp.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpregnancyhelp.setBorderWidth(5);
			imgpregnancyhelp.addShadow();

			imgdomestic.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgdomestic.setBorderWidth(5);
			imgdomestic.addShadow();

			imgpersonalsafety.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpersonalsafety.setBorderWidth(5);
			imgpersonalsafety.addShadow();

			imgstranded.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgstranded.setBorderWidth(5);
			imgstranded.addShadow();

			break;
		case R.id.imgfire:
			imgaccident.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgaccident.setBorderWidth(5);
			imgaccident.addShadow();

			imgcriticallyill.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgcriticallyill.setBorderWidth(5);
			imgcriticallyill.addShadow();

			imgrobbery.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgrobbery.setBorderWidth(5);
			imgrobbery.addShadow();

			imgfire.setBorderColor(getResources().getColor(R.color.skyblue));
			imgfire.setBorderWidth(5);
			imgfire.addShadow();

			imgpregnancyhelp.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpregnancyhelp.setBorderWidth(5);
			imgpregnancyhelp.addShadow();

			imgdomestic.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgdomestic.setBorderWidth(5);
			imgdomestic.addShadow();

			imgpersonalsafety.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpersonalsafety.setBorderWidth(5);
			imgpersonalsafety.addShadow();

			imgstranded.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgstranded.setBorderWidth(5);
			imgstranded.addShadow();

			alert_name = "Fire";
			alert_type = "4";
			break;
		case R.id.imgpregnancyhelp:

			imgaccident.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgaccident.setBorderWidth(5);
			imgaccident.addShadow();

			imgcriticallyill.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgcriticallyill.setBorderWidth(5);
			imgcriticallyill.addShadow();

			imgrobbery.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgrobbery.setBorderWidth(5);
			imgrobbery.addShadow();

			imgfire.setBorderColor(getResources()
					.getColor(R.color.border_color));
			imgfire.setBorderWidth(5);
			imgfire.addShadow();

			imgpregnancyhelp.setBorderColor(getResources().getColor(
					R.color.skyblue));
			imgpregnancyhelp.setBorderWidth(5);
			imgpregnancyhelp.addShadow();

			imgdomestic.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgdomestic.setBorderWidth(5);
			imgdomestic.addShadow();

			imgpersonalsafety.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpersonalsafety.setBorderWidth(5);
			imgpersonalsafety.addShadow();

			imgstranded.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgstranded.setBorderWidth(5);
			imgstranded.addShadow();

			alert_name = "Pregnancy Help";
			alert_type = "5";
			break;
		case R.id.imgdomestic:
			imgaccident.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgaccident.setBorderWidth(5);
			imgaccident.addShadow();

			imgcriticallyill.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgcriticallyill.setBorderWidth(5);
			imgcriticallyill.addShadow();

			imgrobbery.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgrobbery.setBorderWidth(5);
			imgrobbery.addShadow();

			imgfire.setBorderColor(getResources()
					.getColor(R.color.border_color));
			imgfire.setBorderWidth(5);
			imgfire.addShadow();

			imgpregnancyhelp.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpregnancyhelp.setBorderWidth(5);
			imgpregnancyhelp.addShadow();

			imgdomestic
					.setBorderColor(getResources().getColor(R.color.skyblue));
			imgdomestic.setBorderWidth(5);
			imgdomestic.addShadow();

			imgpersonalsafety.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpersonalsafety.setBorderWidth(5);
			imgpersonalsafety.addShadow();

			imgstranded.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgstranded.setBorderWidth(5);
			imgstranded.addShadow();

			alert_name = "Domestic";
			alert_type = "7";
			break;
		case R.id.imgpersonalsafety:
			imgaccident.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgaccident.setBorderWidth(5);
			imgaccident.addShadow();

			imgcriticallyill.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgcriticallyill.setBorderWidth(5);
			imgcriticallyill.addShadow();

			imgrobbery.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgrobbery.setBorderWidth(5);
			imgrobbery.addShadow();

			imgfire.setBorderColor(getResources()
					.getColor(R.color.border_color));
			imgfire.setBorderWidth(5);
			imgfire.addShadow();

			imgpregnancyhelp.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpregnancyhelp.setBorderWidth(5);
			imgpregnancyhelp.addShadow();

			imgdomestic.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgdomestic.setBorderWidth(5);
			imgdomestic.addShadow();

			imgpersonalsafety.setBorderColor(getResources().getColor(
					R.color.skyblue));
			imgpersonalsafety.setBorderWidth(5);
			imgpersonalsafety.addShadow();

			imgstranded.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgstranded.setBorderWidth(5);
			imgstranded.addShadow();

			alert_name = "Personal Safety";
			alert_type = "8";
			break;
		case R.id.imgstranded:
			imgaccident.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgaccident.setBorderWidth(5);
			imgaccident.addShadow();

			imgcriticallyill.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgcriticallyill.setBorderWidth(5);
			imgcriticallyill.addShadow();

			imgrobbery.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgrobbery.setBorderWidth(5);
			imgrobbery.addShadow();

			imgfire.setBorderColor(getResources()
					.getColor(R.color.border_color));
			imgfire.setBorderWidth(5);
			imgfire.addShadow();

			imgpregnancyhelp.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpregnancyhelp.setBorderWidth(5);
			imgpregnancyhelp.addShadow();

			imgdomestic.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgdomestic.setBorderWidth(5);
			imgdomestic.addShadow();

			imgpersonalsafety.setBorderColor(getResources().getColor(
					R.color.border_color));
			imgpersonalsafety.setBorderWidth(5);
			imgpersonalsafety.addShadow();

			imgstranded
					.setBorderColor(getResources().getColor(R.color.skyblue));
			imgstranded.setBorderWidth(5);
			imgstranded.addShadow();

			alert_name = "Stranded";
			alert_type = "6";
			break;
		case R.id.btnFineNow:
			if (InternetUtils.getInstance().isConnectingToInternet(
					getActivity())) {
				if (alert_type.equalsIgnoreCase("")) {
					Toast.makeText(getActivity(), "Please choose alert",
							Toast.LENGTH_LONG).show();
				} else {
					CustomerModel model = CustomerModel.getInstance();
					CustomerDTO cus = model.getCustomer(getActivity());
					AlertDTO dto = new AlertDTO();
					dto.setFrom_customer_number(cus.getMobile_number().trim());
					dto.setCustomer_latitude(String.valueOf(lat));
					dto.setCustomer_longitude(String.valueOf(lng));
					dto.setAlert_type(alert_type);
					dto.setAlert_time(getDateTime());
					dto.setStatus("New");
					dto.setCustomer_location(getAddress(getActivity(), lat, lng));
					dto.setAdditional_notes("abc");

					new VerySlowTask(dto).execute();
				}
			} else {
				Toast.makeText(getActivity(),
						"No network please connect network", Toast.LENGTH_LONG)
						.show();
			}

			break;

		default:
			break;
		}

	}

	public class VerySlowTask extends AsyncTask<Void, Void, Void> {
		String result = "";
		AlertDTO alert = null;
		JSONArray data_body;
		String alert_id = null;
		String lat = "";
		String lon = "";
		ServiceHandler sh = new ServiceHandler();
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		ArrayList<ResponderDTO> arrData = new ArrayList<ResponderDTO>();

		public VerySlowTask(AlertDTO _alert) {
			alert = _alert;
			lat = alert.getCustomer_latitude().replaceAll(",", ".");
			lon = alert.getCustomer_longitude().replaceAll(",", ".");
			dialog = new Dialog(getActivity(), R.style.NewDialog);
			dialog.setContentView(R.layout.loading);

			dialog.setCancelable(false);
			dialog.findViewById(R.id.loading_icon).setBackgroundResource(
					R.drawable.spin_animation);
			frameAnimation = (AnimationDrawable) dialog.findViewById(
					R.id.loading_icon).getBackground();
			dialog.show();
			frameAnimation.start();
		}

		@Override
		protected Void doInBackground(Void... params) {

			nameValuePairs.add(new BasicNameValuePair("query",
					Constants.ADD_ALERT));
			nameValuePairs.add(new BasicNameValuePair("api_secret_key",
					Constants.API_KEY));
			nameValuePairs.add(new BasicNameValuePair(
					Constants.FROM_CUSTOMER_NUMBER, alert
							.getFrom_customer_number()));
			nameValuePairs.add(new BasicNameValuePair(
					Constants.CUSTOMER_LATITUDE, lat));
			nameValuePairs.add(new BasicNameValuePair(
					Constants.CUSTOMER_LONGITUDE, lon));
			nameValuePairs.add(new BasicNameValuePair(Constants.ALERT_TYPE,
					alert.getAlert_type()));
			nameValuePairs.add(new BasicNameValuePair(Constants.ALERT_TIME,
					alert.getAlert_time()));
			nameValuePairs.add(new BasicNameValuePair(Constants.STATUS, alert
					.getStatus()));
			nameValuePairs.add(new BasicNameValuePair(
					Constants.CUSTOMER_LOCATION, alert.getCustomer_location()));
			nameValuePairs.add(new BasicNameValuePair(
					Constants.ADDITIONAL_NOTES, alert.getAdditional_notes()));
			String jsonStr = sh.makeServiceCall(Constants.URL_SERVICE,
					ServiceHandler.POST, nameValuePairs);
			if (jsonStr != null) {
				try {
					JSONObject object;
					object = new JSONObject(jsonStr);
					String value = object.getString("result");
					alert_id = object.getString("alert_id");
					if (value.contains("success")) {
						data_body = object.getJSONArray("data_body");
						// ArrayList<ResponderDTO> arrData = new
						// ArrayList<ResponderDTO>();
						for (int i = 0; i < data_body.length(); i++) {
							ResponderDTO dto = new ResponderDTO();
							JSONObject item = data_body.getJSONObject(i);
							String name = item.getString("responder_name");
							if (!name.equalsIgnoreCase("")) {
								dto.setResponder_name(name);
							}

							arrData.add(dto);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else {
				// Log.e("ServiceHandler",
				// "Couldn't get any data from the url");
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dialog.dismiss();
			if (arrData.size() > 0) {
				AlertSentFragment fragment = new AlertSentFragment();
				FragmentManager fragmentManager = getActivity()
						.getSupportFragmentManager();
				FragmentTransaction ft = fragmentManager.beginTransaction();
				Bundle bundle = new Bundle();
				bundle.putSerializable("data", new ResponderListDTO(arrData));
				bundle.putString("style", alert_name);
				bundle.putString("alert_id", alert_id);
				bundle.putInt("key_center", 1);
				if (fragment != null) {
					fragment.setArguments(bundle);
					ft.remove(fragment);
					ft.replace(R.id.frame_container, fragment);
					ft.commit();
				}

			} else {
				center_err = new Dialog(getActivity(),
						android.R.style.Theme_Translucent);
				center_err.requestWindowFeature(Window.FEATURE_NO_TITLE);

				center_err.setCancelable(true);
				center_err.setContentView(R.layout.dialog_center_err);
				TextView tvtitle_err = (TextView) center_err
						.findViewById(R.id.tvtitle_err);
				TextView tvContent_err = (TextView) center_err
						.findViewById(R.id.tvContent_err);
				tvtitle_err.setText("Can't find center withing 50 miles radios");
				tvContent_err.setText("do you want to send sms to relatives?");
				Button btnyes = (Button) center_err.findViewById(R.id.btnyes);
				Button btnno = (Button) center_err.findViewById(R.id.btnno);

				btnyes.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						AlertSentFragment fragment = new AlertSentFragment();
						FragmentManager fragmentManager = getActivity()
								.getSupportFragmentManager();
						FragmentTransaction ft = fragmentManager
								.beginTransaction();
						Bundle bundle = new Bundle();
						bundle.putSerializable("data", new ResponderListDTO(
								arrData));
						bundle.putString("style", alert_name);
						bundle.putString("alert_id", alert_id);

						if (fragment != null) {
							fragment.setArguments(bundle);
							ft.remove(fragment);
							ft.replace(R.id.frame_container, fragment);
							ft.commit();
						}
						center_err.dismiss();
					}
				});
				btnno.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						center_err.dismiss();

					}
				});

				center_err.show();
			}

		}

	}

	// get address
	public String getAddress(Context ctx, double latitude, double longitude) {
		StringBuilder result = new StringBuilder();
		try {
			Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
			List<Address> addresses = geocoder.getFromLocation(latitude,
					longitude, 1);
			if (addresses.size() > 0) {
				Address address = addresses.get(0);

//				String locality = address.getLocality();
				String country = address.getCountryName();
				String area = address.getAdminArea();
				String subadminarea = address.getSubAdminArea();
				String sublocality = address.getSubLocality();
				
				String featurename = address.getFeatureName();
				
				String region_code = address.getCountryCode();
				String zipcode = address.getPostalCode();
				double lat = address.getLatitude();
				double lon = address.getLongitude();

				result.append(featurename + ", " + sublocality + ", " + subadminarea  + ", " + area + ", " + country);
//				result.append(city + " " + region_code + " ");
//				result.append(zipcode);

			}
		} catch (IOException e) {
			Log.e("tag", e.getMessage());
		}

		return result.toString();
	}

//	private class GetCurrentAddress extends AsyncTask<String, String, String> {
//		String address = "";
//		@Override
//		protected String doInBackground(String... urls) {
//			// this lat and log we can get from current location but here we
//			// given hard coded
//			double latitude = lat;
//			double longitude = lng;
//
//			address = getAddress(getActivity(), latitude, longitude);
////			address = address.replaceAll("null", "");
//			return address;
//		}
//
//		@Override
//		protected void onPostExecute(String resultString) {
//			Address = address;
//		}
//	}

}
